﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LMS.SMSH.MODEL;
using LMS.SMSH.BLL;
using System.Data.Linq;
using System.Data;

namespace LMS.SMSH.WEB
{
    public partial class AdmTeacManange : System.Web.UI.Page
    {
        Int32 rowNo = 0;//行编号
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindGvTeacher();
            }
        }


        private void bindGvTeacher()
        {
            var query = from teacher in LMSDataContext.GetLMSDB.TB_teacher
                        select new
                        {
                            teacher.teacherId,
                            teacher.teacherName,
                            teacher.teacherPwd
                        };
            gvTeacher.DataSource = query;
            gvTeacher.DataKeyNames = new string[] { "teacherId" };
            gvTeacher.DataBind();
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtTeacherName.Text.Trim()))
            {
                var query = from teacher in LMSDataContext.GetLMSDB.TB_teacher
                            where teacher.teacherName.IndexOf(txtTeacherName.Text.Trim()) >= 0
                            select new
                            {
                                teacher.teacherId,
                                teacher.teacherName,
                                teacher.teacherPwd
                            };
                gvTeacher.DataSource = query;
                gvTeacher.DataKeyNames = new string[] { "teacherId" };
                gvTeacher.DataBind(); 
            }
            else
            {
                bindGvTeacher();
            }
        }


        protected void gvTeacher_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                rowNo++;
                e.Row.Cells[0].Text = (gvTeacher.PageIndex * gvTeacher.PageSize + rowNo).ToString();
                //鼠标悬停变色
                e.Row.Attributes.Add("onmouseover", "oldBG=this.style.backgroundColor;this.style.backgroundColor='#FF1199';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=oldBG;");

                Button myDelete = (Button)e.Row.FindControl("btnMyDelete");
                myDelete.Attributes.Add("onclick", "javascript:return confirm('确定删除吗?');");
            }
        }


        protected void gvTeacher_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTeacher.PageIndex = e.NewPageIndex;
            bindGvTeacher();
        }

        /// <summary>
        /// 全选、反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void chkHeader_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow gr in gvTeacher.Rows)
            {
                CheckBox chkItem = (CheckBox)gr.FindControl("chkItem");
                //相互变幻
                chkItem.Checked = !chkItem.Checked;
            }
        }

        protected void btnMyDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gr in gvTeacher.Rows)
            {
                CheckBox chkItem = (CheckBox)gr.FindControl("chkItem");
                if (chkItem.Checked)
                {
                    Int32? result = -1;
                    LMSDataContext.GetLMSDB.sp_deleteDev(gr.Cells[2].Text.Trim(), ref result);
                }
                bindGvTeacher();
            }
        }

        /// <summary>
        /// 新增教师
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminTeacherAdd.aspx");
        }


    }
}